//
//  PictureViewController.m
//  HumorousScripts
//
//  Created by 李欢 on 15/6/15.
//  Copyright (c) 2015年 李欢. All rights reserved.
//

#import "PictureViewController.h"
#import <UIImageView+WebCache.h>
#import "SANetWorkingTask.h"
#import <AFNetworking/AFHTTPRequestOperationManager.h>
#import "PicTableViewCell.h"
#import "MJRefresh.h"
#define Width [UIScreen mainScreen].bounds.size.width
#define Height [UIScreen mainScreen].bounds.size.height

@interface PictureViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, retain) UITableView * tableView;
@property (nonatomic, retain)NSMutableArray * tableArray;
@property (nonatomic, assign) int i;
@end

@implementation PictureViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];

        self.i = 2;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self startConnection];
     
}

#pragma mark -
#pragma mark 请求数据
- (void)startConnection
{
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *str = @"http://api.budejie.com/api/api_open.php?a=list&appname=baisishequ&asid=E2FD8DB1-FBA8-40EA-8C02-FAB446839AD1&c=data&client=iphone&device=ios%20%E8%AE%BE%E5%A4%87&from=ios&jbk=0&mac=&market=&openudid=7c603dda98d9f25b85baf6c3497c6fec45171fb8&page=0&per=20&sub_flag=1&type=10&udid=&ver=3.3";
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    [m GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];
        
        NSArray *array = [dic objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            [self.tableArray addObject:dic];
        }
        NSLog(@"qq%ld", self.tableArray.count);
        
        [self creatView];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
    
}

//创建tableView
-(void)creatView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height - 64/667.0*Height) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    //设定上拉加载 下拉刷新
    [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(loadNewData:)];
    [self.tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:self.tableView];
    
}
//下拉刷新点击事件
- (void)loadNewData:(id)sender
{
    [self.tableArray removeAllObjects];
    [self.tableView reloadData];
    
    [self startConnection];
    [self.tableView.header endRefreshing];
}
//上啦加载点击事件
- (void)loadMoreData
{
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *str = @"http://api.budejie.com/api/api_open.php?a=list&appname=baisishequ&asid=E2FD8DB1-FBA8-40EA-8C02-FAB446839AD1&c=data&client=iphone&device=ios%20%E8%AE%BE%E5%A4%87&from=ios&jbk=0&mac=&market=&openudid=7c603dda98d9f25b85baf6c3497c6fec45171fb8";
    NSString *str1 = [NSString stringWithFormat:@"&page=%d&per=20&sub_flag=1&type=10&udid=&ver=3.3",self.i];
    NSString *str2 = [NSString stringWithFormat:@"%@%@",str,str1];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    [m GET:str2 parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];
        
        
        NSArray *array = [dic objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            [self.tableArray addObject:dic];
        }

        NSLog(@"qq%ld", self.tableArray.count);
        
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
        
    }];
    
    // 拿到当前的上拉刷新控件，结束刷新状态
    
    self.i += 1;
    [self.tableView.footer endRefreshing];
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentify = @"cell";
    PicTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[PicTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify];
    }

    NSDictionary * dic = [self.tableArray objectAtIndex:indexPath.row];
    [cell.imageview sd_setImageWithURL:[dic objectForKey:@"profile_image"] placeholderImage:nil];
    cell.nameLabel.text = [dic objectForKey:@"name"];
    cell.wordsLabel.text = [dic objectForKey:@"text"];
    cell.y = [[dic objectForKey:@"height"] integerValue];

    
//    [cell.LastView.lable1 setText:[NSString stringWithFormat: @"%@",[dic objectForKey:@"love"] ]];
//    cell.LastView1.lable1.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"hate"] ];
//    cell.LastView2.lable1.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"comment"]];
//    cell.share = [dic objectForKey:@"weixin_url"];

    [cell.picView sd_setImageWithURL:[dic objectForKey:@"image1"] placeholderImage:nil];
    [cell setAlabelFrame];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary * dic = [self.tableArray objectAtIndex:indexPath.row];
//    return 90/667.0*Height +  [[dic objectForKey:@"height"] integerValue] + 35/667.0*Height;
    return 90/667.0*Height +  [[dic objectForKey:@"height"] integerValue];
}

//使cell不会保持持续选中状态
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
